package com.jason.bookworm.controller;

import com.jason.bookworm.dao.pojo.User;
import com.jason.bookworm.response.Result;
import com.jason.bookworm.service.LoginService;
import com.jason.bookworm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.http.HttpRequest;
import java.util.Map;

@RestController //表现层类须以@@RestController标记
@RequestMapping("/users")   //@RequestMapping标记访问时的后缀，如：/users表示访问localhost/users
public class UserController {

    @Autowired
    private LoginService loginService;

    //判断是否登录
    @GetMapping
    public Result<Object> isLogin(HttpServletRequest request) {
        String token = request.getHeader("token");
        return loginService.isLogin(token);
    }

    //用户登录
    @PostMapping
    public Result<Object> login(@RequestBody User user) {
        //登录
        return loginService.login(user);
    }

    //退出登录
    @DeleteMapping
    public Result<Object> logout() {
        return loginService.logout();
    }

    //添加新用户
    @PutMapping
    public Result<Object> register(@RequestBody User user){
        return loginService.register(user);
    }

    //修改用户信息
    @PutMapping("/update")
    @Secured("reader")
    public Result<Object> updateUserInfo(@RequestBody User user){
        return loginService.updateUserInfo(user);
    }

}
